var ACRD_ID = 'taste-accordion';

$(function(){
	initTasteGroupHeader($('.accordion > h2'));
	
	$('.accordion').accordion({
		create: function(e, ui) {
			var indx = getAccordionIndex(ACRD_ID);
			$(this).accordion('option', 'active', indx);
			$(this).css('visibility','visible');
		},
		activate: function(e,ui) {
			var indx = $(this).accordion('option', 'active');
			setAccordionIndex(ACRD_ID, indx);
		},
		active: getAccordionIndex(ACRD_ID)
	});
});

function initTasteGroupHeader(headers) {
	headers.dblclick(function(e){
		var h    = $(this);
		if (h.hasClass('editing')) return;
		h.addClass('editing');
		
		var id   = h.attr('data-id');
		var name = h.attr('data-name');
		
		var span = h.children('.label');
		
		$(this).unbind('keydown');
		
		var clone = $('#add-taste-group')
						.clone()
						.show()
						.removeAttr('id');
		span.html(clone)
		clone.data('group-id',   id);
		clone.data('group-name', name);
		setTasteGroupFormEvents(clone);
		
		clone.find('input')[0].focus();
		return false;
	});
}

function setTasteGroupFormEvents(span) {
	var inpt   = span.children('input');
	var save   = span.children('.save');
	var cancel = span.children('.cancel');
	
	var name = span.data('group-name');
	inpt
		.val(name)
		.keydown(function(e){
			var inpt = $(this).siblings('input');
			var code = e.keyCode;
			
			switch (code) {
			case 13:
				$(this).siblings('.action.save').click();
				break;
				
			case 27:
				$(this).siblings('.action.cancel').click();
				break;
				
			default:
				inpt.val(inpt.val() + String.fromCharCode(code));
				break;
			}
			
			return true;
		});
		
	save
		.click(function(){
			var id      = $(this).parent().data('group-id');
			var newName = $(this).siblings('input').val();
			if (newName.length > 0) {
				saveGroup(id, newName);
			} else {
				$(this).siblings('.cancel').click();
			}
		});
	
	cancel
		.click(function(){
			var span = $(this).parent();
			var name = span.data('group-name');
			var h    = span.parent().parent();
			
			h.removeClass('editing');
			if (h.hasClass('ui-accordion-header')) {
				span.html(name);
			} else {
				span.hide();
				$('#add-taste-group-btn').show()
			}
		});
}

function addTasteGroup() {
	var span = $('#add-taste-group');
	setTasteGroupFormEvents(span);
	span
		.show()
		.children('input').focus();
	
	$('#add-taste-group-btn').hide();
}
function editTasteGroup(id, name) {
	var h2 = $('h2.taste-group-' + id);
	h2.dblclick();
}

function saveGroup(groupId, groupName) {
	PUT('/admin_ukusi/save_group', {data: {
		TasteGroup: {
			id:   groupId,
			name: groupName
		}
	}}, function(req){
		hideThrobber();
		if (req.status == 200) {
			location.reload();
		} else {
			httpError(req);
		}
	});
}
function tasteDeleted(id) {
	location.reload();
}
function tasteGroupDeleted(id) {
	$('.taste-group-' + id).remove();
	$('.accordion').accordion('option', 'active', 0);
}

function addTaste(groupId) {
	tasteForm('/admin_ukusi/create/' + groupId, 'Novi Ukus');
}
function editTaste(id) {
	tasteForm('/admin_ukusi/edit/' + id, 'Izmena Ukusa');
}
function tasteForm(url, title) {
	formRemoteDialog(url, {
		'Sačuvaj': function() {
			formDialogSubmit($(this));
		}
	}, {
		title: title,
		height: 550
	});
}